package org.liu.simpleTank;

import java.util.*;

public class Cell {
	// map related
	private Map map;
	private int col;
	private int row;
	public Cell(Map map, int row, int col) {
		this.map = map;
		this.col = col;
		this.row = row;
	}
	
	public int getCol() {
		return this.col;
	}
	
	public int getRow() {
		return this.row;
	}
	
	// cell related
	private static final int upIdx = 0;
	private static final int rightIdx = 1;
	private static final int downIdx = 2;
	private static final int leftIdx = 3;
	private Cell[] cells;
	{
		cells = new Cell[4];
	}
	public void init(Cell up, Cell right, Cell down, Cell left) {
		cells[upIdx] = up;
		cells[rightIdx] = right;
		cells[downIdx] = down;
		cells[leftIdx] = left;
	}
	
	public Cell getUp() {
		return cells[upIdx];
	}
	
	public Cell getRight() {
		return cells[rightIdx];
	}
	
	public Cell getDown() {
		return cells[downIdx];
	}
	
	public Cell getLeft() {
		return cells[leftIdx];
	}
	
	// element related
	private Set<Element> els;
	{
		els = new LinkedHashSet<Element>(4);
	}
	
	public boolean has(Element el) {
		return els.contains(el);
	}
	
	public boolean add(Element el) {
		return els.add(el);
	}
	
	public boolean remove(Element el) {
		return els.remove(el);
	}
	
	public Set<Element> get() {
		return els;
	}
	
	// element related static helper function
	public static boolean cellsHas(Cell[] cells, Element el) {
		if(cells == null || el == null)
			return false;
		for(int i = 0; i < cells.length; i++) {
			if(cells[i].has(el))
				return true;
		}
		return false;
	}
	public static void cellsAdd(Cell[] cells, Element el) {
		if(cells == null || el == null)
			return;
		for(int i = 0; i < cells.length; i++) {
			cells[i].add(el);
		}
	}
	
	public static void cellsRemove(Cell[] cells, Element el) {
		if(cells == null || el == null)
			return;
		for(int i = 0; i < cells.length; i++) {
			cells[i].remove(el);
		}
	}
	
	public static Set<Element> cellsGet(Cell[] cells) {
		LinkedHashSet<Element> set = new LinkedHashSet<Element>();
		for(int i = 0 ; i < cells.length; i++)
			set.addAll(cells[i].get());
		return set;
	}
}
